package com.example.yichuangyun.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.application.MyApplication;
import com.example.yichuangyun.databinding.DialogContactsCompanyBinding;
import com.example.yichuangyun.model.company.CompanyDetailsContactsDataModel;
import com.example.yichuangyun.ui.adapter.EmptyRecyclerCommonAdapter;
import com.example.yichuangyun.ui.adapter.RecyclerViewHolder;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.RequestUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 联系企业
 */
@SuppressLint("NotifyDataSetChanged")
public class ContactsCompanyDialog {
    private final Dialog dialog;
    private final List<String> sourceStr = new ArrayList<>();
    private SinglePickerView singlePickerView;
    private List<CompanyDetailsContactsDataModel.ContactsModel> contactsData;
    private EmptyRecyclerCommonAdapter<CompanyDetailsContactsDataModel.ContactsModel> adapter;
    private List<CompanyDetailsContactsDataModel.ContactsModel> sourceData = new ArrayList<>();
    private int mPosition;

    public ContactsCompanyDialog(Context context, List<CompanyDetailsContactsDataModel.ContactsModel> contactsData, int flag) {
        this.contactsData = contactsData;
        initPickerView(context);
        DialogContactsCompanyBinding binding = DialogContactsCompanyBinding.inflate(LayoutInflater.from(context));
        dialog = new Dialog(context, R.style.prompt_progress_dialog_dimenabled);
        dialog.setContentView(binding.getRoot(), new LinearLayout.LayoutParams
                (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        dialog.setCancelable(true);
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.BOTTOM);
        // 默认展示--创建一个展示一个，显示在之前的Dialog上面--WindowManager.LayoutParams.TYPE_APPLICATION
//         dialogWindow.setType(WindowManager.LayoutParams.TYPE_APPLICATION);
        // 低优先级----显示在之前的Dialog下面--WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG
//        dialogWindow.setType(WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG);
        dialogWindow.setWindowAnimations(R.style.main_menu_animStyle);
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialogWindow.setAttributes(lp);

        adapter = new EmptyRecyclerCommonAdapter<CompanyDetailsContactsDataModel.ContactsModel>
                (context, R.layout.item_dialog_contacts_company, contactsData) {
            @SuppressLint("ClickableViewAccessibility")
            @Override
            public void convert(RecyclerViewHolder holder, CompanyDetailsContactsDataModel.ContactsModel item, int position) {
                holder.setText(R.id.tv_contact_way, item.getNumber());
                holder.setText(R.id.tv_name, CommonUtil.setText3(item.getName()));
                TextView tvRemark = holder.getView(R.id.tv_remark);
                tvRemark.setMovementMethod(ScrollingMovementMethod.getInstance());
                tvRemark.setOnTouchListener((v, event) -> {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        //通知父控件不要干扰,即屏蔽父控件的该事件以及该事件之后的一切action
                        v.getParent().requestDisallowInterceptTouchEvent(true);
                    }
                    if (event.getAction() == MotionEvent.ACTION_MOVE) {
                        //通知父控件不要干扰,即屏蔽父控件的该事件以及该事件之后的一切action
                        v.getParent().requestDisallowInterceptTouchEvent(true);
                    }
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                    }
                    return false;
                });
                List<CompanyDetailsContactsDataModel.ContactsModel> list = new ArrayList<>();
                if (null != item.getList()) {
                    list = item.getList();
                }
                // 1手机 2固定电话 3email
                switch (flag) {
                    case 1:
                        holder.setViewVisibility(R.id.ll_add_contacts, item.getIsShowAddButton() == 1 ? View.VISIBLE : View.GONE);
                        holder.setViewVisibility(R.id.ll_contact_now, item.getIsPublic() == 1 ? View.VISIBLE : View.GONE);

                        if (item.getIsPublic() == 1) {
                            holder.setViewVisibility(R.id.ll_contact_now, View.VISIBLE);
                        } else {
                            if (item.getCreateUserId().equals(MyApplication.manager.getId())) {
                                holder.setViewVisibility(R.id.ll_contact_now, View.VISIBLE);
                            } else {
                                holder.setViewVisibility(R.id.ll_contact_now, View.GONE);
                            }
                        }
                        holder.setText(R.id.tv_remark, CommonUtil.setText3(item.getRemark()));
                        holder.setText(R.id.tv_source, item.getIsManuaal() == 1 ? context.getString(R.string.company_contacts_source, item.getSource()) : item.getSource());
                        break;
                    case 2:
                        holder.setViewVisibility(R.id.ll_add_contacts, View.GONE);
                        holder.setText(R.id.tv_source, item.getIsManuaal() == 1 ? context.getString(R.string.company_contacts_source, item.getSource()) : item.getSource());
                        break;
                    case 3:
                        holder.setViewVisibility(R.id.tv_remark, View.GONE);
                        holder.setViewVisibility(R.id.ll_add_contacts, View.GONE);
                        holder.setViewVisibility(R.id.ll_contact_now, View.GONE);
                        holder.setText(R.id.tv_source, item.getSource());
                        break;
                    case 4:
                        holder.setViewVisibility(R.id.ll_container, View.GONE);
                        holder.setViewVisibility(R.id.tv_contacts_now, View.VISIBLE);
                        holder.setText(R.id.tv_contact_way, item.getValue());
                        holder.setText(R.id.tv_remark, item.getIsManuaal() == 1 ? context.getString(R.string.company_contacts_source, item.getSource()) : item.getSource());
                        holder.setOnClickListener(R.id.tv_contacts_now, view -> {
                            RequestUtils.callNum(context, item.getValue());
                            dismissDialog();
                        });
                        break;
                }
                holder.setViewEnable(R.id.ll_source, list.size() > 1);
                holder.setViewVisibility(R.id.iv_down, list.size() > 1 ? View.VISIBLE : View.GONE);
            }

            @Override
            public void bindClick(RecyclerViewHolder holder, CompanyDetailsContactsDataModel.ContactsModel item, int position) {
                holder.setOnClickListener(R.id.ll_source, view -> {
                    if (null != item.getList() && item.getList().size() > 0) {
                        mPosition = position;
                        sourceData = item.getList();
                        sourceStr.clear();
                        for (CompanyDetailsContactsDataModel.ContactsModel model : item.getList()) {
                            sourceStr.add(model.getIsManuaal() == 1 ? context.getString(R.string.company_contacts_source, model.getSource()) : model.getSource());
                        }
                        singlePickerView.setData(sourceStr);
                        singlePickerView.showSinglePickerView();
                    }
                });

                holder.setOnClickListener(R.id.ll_contact_now, view -> {
                    dismissDialog();
                    RequestUtils.callNum(context, item.getNumber());
                });
                holder.setOnClickListener(R.id.ll_add_contacts, view -> {
                    dismissDialog();
                    addAddressBook(item);
                });
            }
        };
        binding.recyclerView.setAdapter(adapter);
        binding.ivClose.setOnClickListener(v -> dismissDialog());
    }

    public void addAddressBook(CompanyDetailsContactsDataModel.ContactsModel contactsModel) {

    }

    public void refreshData(List<CompanyDetailsContactsDataModel.ContactsModel> contactsData) {
        this.contactsData = contactsData;
        adapter.notifyDataSetChanged();
    }

    public void showDialog() {
        dialog.show();
    }

    public void dismissDialog() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }

    public void initPickerView(Context context) {
        singlePickerView = new SinglePickerView((Activity) context, "", sourceStr, true) {
            @Override
            public void confirm(String label, int pos) {
                contactsData.get(mPosition).setSource(sourceData.get(pos).getSource());
                contactsData.get(mPosition).setName(sourceData.get(pos).getName());
                contactsData.get(mPosition).setRemark(sourceData.get(pos).getRemark());
                contactsData.get(mPosition).setIsManuaal(sourceData.get(pos).getIsManuaal());
                adapter.notifyItemChanged(mPosition);
            }
        };
    }
}
